package com.wchat.server.handler;

import com.wchat.bean.model.MsgPayload;
import com.wchat.bean.model.Session;
import com.wchat.bean.model.body.AckBody;
import com.wchat.bean.model.body.ChatMsgBody;
import com.wchat.bean.model.enums.HeaderTypeEm;
import com.wchat.bean.model.enums.TypeEm;
import com.wchat.mapper.FriendMsgsMapper;
import com.wchat.mapper.GroupMsgsMapper;
import com.wchat.server.ClientManager;
import com.wchat.utils.SnowFlakeUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.stereotype.Component;

/**
 * @author : willian fu
 * @date : 2021/8/30
 */
@Slf4j
@Component
@EnableAsync
public class PackageHandler {

    @Autowired
    private ClientManager clientManager;

    @Autowired
    private FriendMsgsMapper friendMsgsMapper;

    @Autowired
    private GroupMsgsMapper groupMsgsMapper;



    @Async
    public void handler(Session session, MsgPayload payload) {
        switch (payload.getHeader()) {
            case PING:
                break;
            case ACK:
                ackHandler(session.getUserId(), payload);
                break;
            case CHAT:
                chatHandler(session, payload);
                break;
            case EVENT:
                eventHandler(payload);
                break;
            default:
                break;
        }
    }

    private void ackHandler(Integer from, MsgPayload payload) {
        switch (payload.getType()) {
            //聊天消息收到应答
            case CHAT_MSG_ACK:
                //删除对应的消息缓存
                cleanMsgBuffer(from, payload.getMsgId());
                break;
            //心跳应答
            case PING_ACK:
                break;
            default:
                break;

        }
    }

    private void chatHandler(Session session, MsgPayload payload) {
        ChatMsgBody body = (ChatMsgBody) payload.getBody();
        Integer targetUser = body.getUserId();
                body.setMsgId(String.valueOf(SnowFlakeUtil.getInstance().nextId(false)));
        switch (payload.getType()) {
            //聊天消息
            case CHAT_MSG:
                boolean ok = false;
                body.setUserId(session.getUserId());
                body.setTime(System.currentTimeMillis());
                if (payload.isGroup()){//鉴权
                    if (ClientManager.getGroups().get(body.getGroupId()).contains(session.getUserId())){
                        clientManager.sendChatMsgToGroupQos2(session.getUserId(), body.getGroupId(), payload);
                        ok = true;
                    }
                }else {
                    if (session.getFriends().contains(targetUser)){
                        clientManager.sendChatMsgQos2(targetUser, payload);
                        ok = true;
                    }
                }
                if (ok){
                    clientManager.sendMsg(session.getUserId(),
                            MsgPayload.builder()
                                    .header(HeaderTypeEm.ACK)
                                    .type(TypeEm.CHAT_MSG_ACK)
                                    .body(AckBody.builder()
                                            .msgId(body.getMsgId())
                                            .success(true)
                                            .message("success")
                                            .build())
                                    .build());
                }else {
                    clientManager.sendMsg(session.getUserId(),
                            MsgPayload.builder()
                                    .header(HeaderTypeEm.ACK)
                                    .type(TypeEm.CHAT_MSG_ACK)
                                    .body(AckBody.builder()
                                            .success(false)
                                            .message("No Authority!")
                                            .build())
                                    .build());
                }
                break;
            default:
                break;
        }
    }

    private void eventHandler(MsgPayload payload) {
        switch (payload.getType()) {
            //群组用户变更事件，有用户进群/退群/更新信息
            case GROUP_USER_CHANGE:
                break;
            //群信息有更新 如修改群名，群备注等
            case GROUP_INFO_CHANGE:
                break;
            //加入群事件
            case GROUP_JOIN:
                break;
            //被踢出群事件
            case GROUP_LEAVE:
                break;
            //好友资料更新
            case FRIEND_INFO_CHANGE:
                break;
            //被申请添加好友
            case FRIEND_ADD_REQ:
                break;
            //被添加为好友
            case FRIEND_ADD:
                break;
            //被删除好友关系
            case FRIEND_DEL:
                break;
            //被拉黑
            case FRIEND_BLACKLIST:
                break;
            //消息被撤回
            case MSG_WITHDRAW:
                break;
            //被强制下线/被挤下线
            case SYS_OFFLINE:
                break;
            default:
                break;
        }
    }

    private void cleanMsgBuffer(Integer targetId, Long msgId){
        clientManager.cleanMsgCatch(targetId, msgId);
    }
}
